package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.activities.SearchActivity;
import com.zadtek.shopmall.adapter.TypeAdapter;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.MainType;
import com.zadtek.shopmall.bean.SectionTypeBean;
import com.zadtek.shopmall.bean.SubType;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zackv on 2018/4/2.
 */

public class TypeFragment extends AbsFragment {
    private TextView leftText;
    private ImageView rightImg;
    private EditText editSearch;
    private LinearLayout typeContainer;
    private RecyclerView recyclerView;

    private List<MainType> mainTypes = new LinkedList<>();
    private List<SubType> currentSubTypes = new LinkedList<>();
    private List<SectionTypeBean> sectionTypeBeans = new LinkedList<>();
    private List<TextView> viewList = new LinkedList<>();
    private TypeAdapter typeAdapter;
    @Override
    public int setView() {
        return R.layout.fragment_type;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getcategory(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<MainType>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<MainType>> value) {
                        if(value.getCode() == 0){
                            mainTypes.clear();
                            mainTypes.addAll(value.getData());
                            updateLeft();
                            setCurrentType(0);
                        }
                    }
                });
    }

    private void setCurrentType(int index){
        for(TextView t:viewList){
            t.setTextColor(getResources().getColor(R.color.color_black));
            t.setBackgroundColor(getResources().getColor(R.color.transparent));
        }
        viewList.get(index).setTextColor(getResources().getColor(R.color.colorAccent));
        viewList.get(index).setBackgroundColor(getResources().getColor(R.color.color_white));
        currentSubTypes.clear();
        currentSubTypes.addAll(mainTypes.get(index).getSubTypes());
        updateRight();
    }

    private void updateLeft(){
        if(!viewList.isEmpty()) {
            viewList.clear();
            typeContainer.removeAllViews();
        }
        for(int i=0;i<mainTypes.size();i++){
            final View v = getLayoutInflater().inflate(R.layout.main_type_item,null);
            TextView typeName = v.findViewById(R.id.type_name);
            typeName.setText(mainTypes.get(i).getMainTypeName());
            viewList.add(typeName);
            final int finalI = i;
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setCurrentType(finalI);
                }
            });
            typeContainer.addView(v);
        }
    }
    private void updateRight(){
        if(!sectionTypeBeans.isEmpty())sectionTypeBeans.clear();
        for(SubType subType:currentSubTypes){
            SectionTypeBean bean = new SectionTypeBean(true,subType.getTitle());
            sectionTypeBeans.add(bean);
            for(SubType.SubTypeItem subTypeItem:subType.getSubTypeItems()){
                SectionTypeBean bean2 = new SectionTypeBean(subTypeItem);
                sectionTypeBeans.add(bean2);
            }
        }
        typeAdapter.notifyDataSetChanged();
    }
    private void initLogic() {
        typeAdapter = new TypeAdapter(mActivity,R.layout.sub_type_item,R.layout.sub_type_title,sectionTypeBeans);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SectionTypeBean bean = sectionTypeBeans.get(position);
                if(!bean.isHeader){
                    SubType.SubTypeItem subTypeItem = bean.t;
                    //subTypeItem.setTypeId(true);
                    subTypeItem.setType(Const.PRODUCT_LIST);
                    ToastUtil.showToast("subtypeitem"+subTypeItem.toString()+position);
                    Const.jumpHandle(mActivity,subTypeItem);
                }
            }
        });
        editSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, SearchActivity.class));
            }
        });
        recyclerView.setAdapter(typeAdapter);
    }

    private void initData() {
        SubType subType = new SubType();
        SubType.SubTypeItem subTypeItem = new SubType.SubTypeItem();
        subTypeItem.setId(322);
        subTypeItem.setType(Const.PRODUCT_LIST);
        subTypeItem.setTypeId(1);
        subTypeItem.setTypeImg("http://tc.zadtek.com/aaa/images/201603/thumb_img/530_thumb_G_1457132840648.jpg");
        subTypeItem.setTypeName("平板电视");

        SubType.SubTypeItem subTypeItem2 = new SubType.SubTypeItem();
        subTypeItem2.setId(43543);
        subTypeItem2.setType(Const.PRODUCT_LIST);
        subTypeItem2.setTypeId(1);
        subTypeItem2.setTypeImg("http://tc.zadtek.com/aaa/images/201512/thumb_img/303_thumb_G_1450030210059.jpg");
        subTypeItem2.setTypeName("空调");

        List<SubType.SubTypeItem> typeItems = new LinkedList<>();
        typeItems.add(subTypeItem);
        typeItems.add(subTypeItem2);
        typeItems.add(subTypeItem);
        typeItems.add(subTypeItem2);
        typeItems.add(subTypeItem);
        typeItems.add(subTypeItem2);

        subType.setSubTypeItems(typeItems);
        subType.setTitle("大家电");
        List<SubType> subTypeList = new LinkedList<>();
        subTypeList.add(subType);
        subTypeList.add(subType);
        subTypeList.add(subType);
        subTypeList.add(subType);



        MainType mainType = new MainType();
        mainType.setMainTypeId(1);
        mainType.setMainTypeName("家用电器");
        mainType.setSubTypes(subTypeList);

        mainTypes.add(mainType);

        SubType subType2 = new SubType();
        SubType.SubTypeItem subTypeItem3 = new SubType.SubTypeItem();
        subTypeItem3.setType(Const.PRODUCT_LIST);
        subTypeItem3.setId(232323);
        subTypeItem3.setTypeId(353454353);
        subTypeItem3.setTypeImg("http://tc.zadtek.com/aaa/images/201501/thumb_img/223_thumb_G_1422573046021.jpg");
        subTypeItem3.setTypeName("连衣裙");

        SubType.SubTypeItem subTypeItem4 = new SubType.SubTypeItem();
        subTypeItem4.setTypeId(343543);
        subTypeItem4.setId(343543);
        subTypeItem4.setType(Const.PRODUCT_LIST);
        subTypeItem4.setTypeImg("http://tc.zadtek.com/aaa/images/201512/thumb_img/14_thumb_G_1450311204408.jpg");
        subTypeItem4.setTypeName("半膝裙");

        List<SubType.SubTypeItem> typeItems2 = new LinkedList<>();
        typeItems2.add(subTypeItem3);
        typeItems2.add(subTypeItem4);
        typeItems2.add(subTypeItem3);
        typeItems2.add(subTypeItem4);
        typeItems2.add(subTypeItem3);
        typeItems2.add(subTypeItem4);

        subType2.setSubTypeItems(typeItems2);
        subType2.setTitle("女装");
        List<SubType> subTypeList2 = new LinkedList<>();
        subTypeList2.add(subType2);
        subTypeList2.add(subType2);
        subTypeList2.add(subType2);
        subTypeList2.add(subType2);

        MainType mainType2 = new MainType();
        mainType2.setMainTypeId(1);
        mainType2.setMainTypeName("男装女装");
        mainType2.setSubTypes(subTypeList2);
        mainTypes.add(mainType2);

        Gson gson = new Gson();
        BaseResponse<List<MainType>> baseResponse = new BaseResponse();
        baseResponse.setCode(0);
        baseResponse.setMessage("");
        baseResponse.setData(mainTypes);
        String string = gson.toJson(baseResponse);

    }

    private void initView() {
        leftText = (TextView) findViewById(R.id.left_text);
        rightImg = (ImageView) findViewById(R.id.right_icon);
        leftText.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        editSearch = (EditText) findViewById(R.id.search_edit);
        editSearch.setFocusable(false);
        typeContainer = (LinearLayout) findViewById(R.id.main_type_container);
        recyclerView = (RecyclerView) findViewById(R.id.type_detail_container);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity,3);
        recyclerView.setLayoutManager(gridLayoutManager);
    }


}
